<template>
  <nav-bar :title="`常用${adressText}地址`" :whiteStyle="true" :back="false">
    <template v-slot:left>
      <div>
        <svg-icon name="back-black" @click="enterBeforePage"></svg-icon>
      </div>
    </template>
  </nav-bar>

  <div class="main">
    <van-list
      v-model:loading="initData.loading"
      :finished="initData.finished"
      finished-text="没有更多了"
      v-model:error="initData.error"
      error-text="请求失败，点击重新加载"
      :immediate-check="false"
      @load="onLoad"
    >
      <van-swipe-cell v-for="(item, i) in initData.list" :key="i">
        <van-row class="adressItem" @click="handleSelect(item)">
          <van-col span="3" class="icon">
            <svg-icon :name="'location-msg'"></svg-icon>
          </van-col>
          <van-col span="21">
            <div class="detail">
              {{ item.provinceName + item.cityName + item.countyName + item.detailAddress }}
            </div>
            <div class="contact">
              {{ item.consigneeConsignor }}
              <span>{{ item.consigneeConsignorPhone }}</span>
            </div>
          </van-col>
        </van-row>
        <template #right>
          <div class="delete">
            <div class="button">
              <svg-icon name="delete" @click="handleDelete(item.id)"></svg-icon>
            </div>
          </div>
        </template>
      </van-swipe-cell>
    </van-list>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import navBar from '@/components/NavBar.vue'
import { api } from '@/http/api'
import http from '@/http'
import { deepClone } from '@/utils/utils'

interface InitState {
  list: any[]
  pageNo: number
  loading: boolean
  error: boolean
  finished: boolean
}

export default defineComponent({
  name: 'AddressList',
  components: {
    navBar
  },
  setup() {
    const route = useRoute()
    const router = useRouter()

    const initData: InitState = {
      list: [],
      pageNo: 1,
      loading: false, //下拉加载
      error: false, //下拉加载失败
      finished: false //触底
    }

    const deepData = deepClone(initData)

    const state = reactive({
      initData: deepData,
      addressType: 1 //地址类型(1发货地 2收货地)
    })

    const addressInfo = route.params.addressInfo
    const sendType = route.params.type

    let adressText = '装货'
    if (sendType === 'start') {
      state.addressType = 1
    } else {
      state.addressType = 2
      adressText = '卸货'
    }

    // 返回上一页返还参数
    const enterBeforePage = () => {
      router.push({
        name: 'AddressInfo',
        params: { type: sendType, adderssInfo: addressInfo }
      })
    }

    // 加载列表数据
    const onLoad = (refresh = false) => {
      if (refresh) {
        state.initData.pageNo = 1
        state.initData.loading = true
        state.initData.finished = false
      }
      const params = {
        pageNo: state.initData.pageNo,
        pageSize: 10,
        addressType: state.addressType
      }
      http<string>({
        url: api.transportAdress,
        method: 'get',
        params
      })
        .then((res: any) => {
          // 加载状态结束
          state.initData.loading = false
          if (refresh) {
            state.initData.list = res.rows
          } else {
            state.initData.list = state.initData.list.concat(res.rows)
          }
          if (res.pageNo === res.totalPage) {
            state.initData.finished = true
          }
          //页码要增加1
          state.initData.pageNo++
        })
        .catch(err => {
          // 加载状态结束
          state.initData.loading = false
          //下拉失败产生点击重新加载提示
          state.initData.error = true
        })
    }

    //删除货主常用地址
    const handleDelete = (id: string) => {
      http<string>({
        url: api.deleteTransportAdress,
        method: 'post',
        data: {
          id,
          addressType: state.addressType
        }
      }).then(() => {
        onLoad(true)
      })
    }

    // 选中地址
    const handleSelect = (val: any) => {
      router.push({ name: 'AddressInfo', params: { type: sendType, addressOption: JSON.stringify(val) } })
    }

    onMounted(() => {
      onLoad()
    })

    return {
      ...toRefs(state),
      enterBeforePage,
      onLoad,
      handleDelete,
      handleSelect,
      adressText
    }
  }
})
</script>
<style lang="scss" scoped>
@import '@/style/variables.scss';
@import '@/style/mixins.scss';
.main {
  padding: 15px 12px 30px;
  box-sizing: border-box;
  // min-height: $main-min-height;
  // background: $background-color-base;
  // position: relative;

  .adressItem {
    background: #ffffff;
    border-radius: 8px;
    padding: 20px 10px;
    box-sizing: border-box;
    margin-bottom: 12px;
    .icon {
      text-align: center;
      width: 20px;
      height: 20px;
      line-height: 30px;
    }
    .detail {
      @include ellipsis;
      line-height: 30px;
      font-size: 15px;
    }
    .contact {
      font-size: 13px;
      color: #575c72;
      span {
        margin-left: 10px;
      }
    }
  }

  .delete {
    width: 68px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .button {
      width: 46px;
      height: 46px;
      background: #ffffff;
      box-shadow: 0px 4px 8px 0px rgba(143, 143, 143, 0.11);
      border-radius: 23px;
      text-align: center;
      line-height: 46px;
    }
  }
}
</style>
